package com.blackandwhitesensation.sice.presentation.backingBeans;

import com.blackandwhitesensation.sice.exceptions.*;
import com.blackandwhitesensation.sice.modelo.*;
import com.blackandwhitesensation.sice.modelo.dto.TipoUsuarioSistemaDTO;
import com.blackandwhitesensation.sice.presentation.businessDelegate.BusinessDelegatorView;
import com.blackandwhitesensation.sice.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 *
 */
public class TipoUsuarioSistemaView {
    private InputText txtValorTipoUsuarioSistema;
    private InputText txtIdTipoUsuarioSistema;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoUsuarioSistemaDTO> data;
    private TipoUsuarioSistemaDTO selectedTipoUsuarioSistema;

    public TipoUsuarioSistemaView() {
        super();
    }
    
    public String actionCerrarSesion(){
    	FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("usuarioSistema");
    	return "goSalir";
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoUsuarioSistemaDTO tipoUsuarioSistemaDTO = (TipoUsuarioSistemaDTO) e.getObject();

            if (txtValorTipoUsuarioSistema == null) {
                txtValorTipoUsuarioSistema = new InputText();
            }

            txtValorTipoUsuarioSistema.setValue(tipoUsuarioSistemaDTO.getValorTipoUsuarioSistema());

            if (txtIdTipoUsuarioSistema == null) {
                txtIdTipoUsuarioSistema = new InputText();
            }

            txtIdTipoUsuarioSistema.setValue(tipoUsuarioSistemaDTO.getIdTipoUsuarioSistema());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtValorTipoUsuarioSistema != null) {
            txtValorTipoUsuarioSistema.setValue(null);
            txtValorTipoUsuarioSistema.setDisabled(true);
        }

        if (txtIdTipoUsuarioSistema != null) {
            txtIdTipoUsuarioSistema.setValue(null);
            txtIdTipoUsuarioSistema.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        TipoUsuarioSistema entity = null;

        try {
            Long idTipoUsuarioSistema = new Long(txtIdTipoUsuarioSistema.getValue()
                                                                        .toString());
            entity = BusinessDelegatorView.getTipoUsuarioSistema(idTipoUsuarioSistema);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtValorTipoUsuarioSistema.setDisabled(false);
            txtIdTipoUsuarioSistema.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtValorTipoUsuarioSistema.setValue(entity.getValorTipoUsuarioSistema());
            txtValorTipoUsuarioSistema.setDisabled(false);
            txtIdTipoUsuarioSistema.setValue(entity.getIdTipoUsuarioSistema());
            txtIdTipoUsuarioSistema.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveTipoUsuarioSistema(FacesUtils.checkLong(
                    txtIdTipoUsuarioSistema),
                FacesUtils.checkString(txtValorTipoUsuarioSistema));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteTipoUsuarioSistema(FacesUtils.checkLong(
                    txtIdTipoUsuarioSistema));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateTipoUsuarioSistema(FacesUtils.checkLong(
                    txtIdTipoUsuarioSistema),
                FacesUtils.checkString(txtValorTipoUsuarioSistema));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtIdTipoUsuarioSistema == null) {
                txtIdTipoUsuarioSistema = new InputText();
            }

            txtIdTipoUsuarioSistema.setValue(selectedTipoUsuarioSistema.getIdTipoUsuarioSistema());

            BusinessDelegatorView.deleteTipoUsuarioSistema(FacesUtils.checkLong(
                    txtIdTipoUsuarioSistema));
            data.remove(selectedTipoUsuarioSistema);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long idTipoUsuarioSistema,
        String valorTipoUsuarioSistema) throws Exception {
        try {
            BusinessDelegatorView.updateTipoUsuarioSistema(idTipoUsuarioSistema,
                valorTipoUsuarioSistema);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoUsuarioSistemaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtValorTipoUsuarioSistema() {
        return txtValorTipoUsuarioSistema;
    }

    public void setTxtValorTipoUsuarioSistema(
        InputText txtValorTipoUsuarioSistema) {
        this.txtValorTipoUsuarioSistema = txtValorTipoUsuarioSistema;
    }

    public InputText getTxtIdTipoUsuarioSistema() {
        return txtIdTipoUsuarioSistema;
    }

    public void setTxtIdTipoUsuarioSistema(InputText txtIdTipoUsuarioSistema) {
        this.txtIdTipoUsuarioSistema = txtIdTipoUsuarioSistema;
    }

    public List<TipoUsuarioSistemaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataTipoUsuarioSistema();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoUsuarioSistemaDTO> tipoUsuarioSistemaDTO) {
        this.data = tipoUsuarioSistemaDTO;
    }

    public TipoUsuarioSistemaDTO getSelectedTipoUsuarioSistema() {
        return selectedTipoUsuarioSistema;
    }

    public void setSelectedTipoUsuarioSistema(
        TipoUsuarioSistemaDTO tipoUsuarioSistema) {
        this.selectedTipoUsuarioSistema = tipoUsuarioSistema;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
